package com.kadron.favorites;

import com.kadron.common.KUtil;
import com.kadron.dao.KImageDAO;
import com.kadron.dao.KUserFavoritesDAO;
import com.kadron.datamodel.KModelCreator;
import com.kadron.entity.KItemImage;
import com.kadron.entity.UserFavorites;

public class KFavoriteItemModelCreator implements KModelCreator {

	/**
	 * 
	 */
	private static final long serialVersionUID = -5431750255930389850L;

	private KUserFavoritesDAO userFavoritesDAO;
	private KImageDAO imageDAO;
	
	public KFavoriteItemModelCreator(KUserFavoritesDAO userFavoritesDAO, KImageDAO imageDAO) {
		super();
		this.userFavoritesDAO = userFavoritesDAO;
		this.imageDAO = imageDAO;
	}

	@Override
	public KFavoriteItemModel create(long favId) {
		UserFavorites fav = userFavoritesDAO.find(favId, UserFavorites.class);
		KItemImage img = imageDAO.getMainItemImage(fav.getItem().getId());
		String thumb = "";
		if (img != null && !KUtil.isEmpty(img.getThumb())) {
			thumb = img.getThumb();
		}
		KFavoriteItemModel model = new KFavoriteItemModel(favId, fav.getUser().getId(), fav.getUser().getUsername(), fav.getCreated(), fav.getItem(), thumb);
		return model;
	}
	
}
